import { reqLogin } from '../../api/index2'
import { saveToken, saveUser } from '../../utils/storageUtils'


// 给表单绑定submit监听
document.getElementById('loginForm').onsubmit = async (e) => {
  // 阻止事件的默认行为
  e.preventDefault()

  // 得到输入的username和pwd
  const username = document.getElementById('username').value
  const pwd = document.getElementById('pwd').value
  console.log(username, pwd)

  // 发登陆的ajax请求
  const result = await reqLogin(username, pwd)

  // 失败, 提示
  if (result.code!=='2000') {
    alert(result.message)
  } else {
    // 成功后, 保存token和user  跳转到用户列表
    const {token, user} = result.data
    saveToken(token)
    saveUser(user)
    window.location = '/users.html'
  }
}


